package callnumber.gtdev5.com.photoVault.activity;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.base.FristBaseActivity;
import callnumber.gtdev5.com.photoVault.bean.PasswordBean;

import static com.gtdev5.geetolsdk.mylibrary.util.ToastUtils.showShortToast;

public class FirstMainActivity extends FristBaseActivity implements View.OnClickListener {
    Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_pt;
    Button btn_mul, btn_div, btn_add, btn_sub, btn_percent;
    Button btn_clr, btn_del, btn_eq;
    EditText et_input, et_input1;
    boolean clr_flag; //判断et编辑文本框中是否清空
    List<PasswordBean> datas;

    @Override
    protected int getLayouId() {
        return R.layout.activity_first_main;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        btn_0 = findViewById(R.id.btn_0);
        btn_1 = findViewById(R.id.btn_1);
        btn_2 = findViewById(R.id.btn_2);
        btn_3 = findViewById(R.id.btn_3);
        btn_4 = findViewById(R.id.btn_4);
        btn_5 = findViewById(R.id.btn_5);
        btn_6 = findViewById(R.id.btn_6);
        btn_7 = findViewById(R.id.btn_7);
        btn_8 = findViewById(R.id.btn_8);
        btn_9 = findViewById(R.id.btn_9);
        btn_pt = findViewById(R.id.btn_pt);
        btn_add = findViewById(R.id.btn_add);
        btn_sub = findViewById(R.id.btn_sub);
        btn_mul = findViewById(R.id.btn_mul);
        btn_div = findViewById(R.id.btn_div);
        btn_clr = findViewById(R.id.btn_clr);
        btn_del = findViewById(R.id.btn_del);
        btn_eq = findViewById(R.id.btn_eq);
        et_input = findViewById(R.id.et_input);
        et_input1 = findViewById(R.id.et_input1);
        btn_percent = findViewById(R.id.btn_percent);
        //给按钮设置的点击事件
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_pt.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_mul.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_clr.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_eq.setOnClickListener(this);
        btn_percent.setOnClickListener(this);

        date();
    }

    SharedPreferences.Editor editor;

    private void date() {
        SharedPreferences shared = getSharedPreferences("is", MODE_PRIVATE);
        boolean isfer = shared.getBoolean("isfer", true);
        editor = shared.edit();
        if (isfer) {
            //第一次进入跳转
            showWeiXinDialog();

        } else {
            //第二次
        }
    }
    //editor

    private void showWeiXinDialog() {

        try {
            final Dialog dialog = new Dialog(this);
            dialog.setCancelable(false);
            dialog.show();
            Window window = dialog.getWindow();
            window.setContentView(R.layout.jclq_prompt_dialog);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            CheckBox cb_jclq_tishi = (CheckBox) window.findViewById(R.id.cb_jclq_tishi);
            cb_jclq_tishi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    if (isChecked) {
//                        showShortToast("1");
                        editor.putBoolean("isfer", false);
                        editor.commit();
                    } else {
//                        showShortToast("2");
                    }
                }
            });
            LinearLayout llayout_dialgo_jclq_yes = (LinearLayout) window.findViewById(R.id.llayout_dialgo_jclq_yes);
            llayout_dialgo_jclq_yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void initDatas() {

    }

    @Override
    protected void initAction() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    boolean poss = true;
    String firstPoss;

    @Override
    public void onClick(View v) {
        String str = et_input.getText().toString();
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_pt:
                if (clr_flag) {
                    clr_flag = false;
                    str = "";
                    et_input.setText("");
                }
                et_input.setText(str + ((Button) v).getText());
                break;
            case R.id.btn_add:
            case R.id.btn_sub:
            case R.id.btn_mul:
            case R.id.btn_div:
                if (TextUtils.isEmpty(et_input.getText().toString())) {
                    str = "";
                    et_input.setText("");
                    return;
                }
                if (clr_flag) {
                    clr_flag = false;
                    str = "";
                    et_input.setText("");
                }
                if (str.contains("+") || str.contains("-") || str.contains("×") || str.contains("÷")) {
                    str = str.substring(0, str.indexOf(" "));
                }
                et_input.setText(str + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_clr:
                if (clr_flag)
                    clr_flag = false;
                str = "";
                et_input.setText("");
                et_input1.setText("");
                break;
            case R.id.btn_del: //判断是否为空，然后在进行删除
                if (clr_flag) {
                    clr_flag = false;
                    str = "";
                    et_input.setText("");
                } else if (str != null && !str.equals("")) {
                    et_input.setText(str.substring(0, str.length() - 1));
                }
                break;
            case R.id.btn_eq: //单独运算最后结果
                getResult();//调用下面的方法
                break;
            case R.id.btn_percent:
                if (str.contains("＋") || str.contains("－") || str.contains("×") || str.contains("÷")) {
                    return;
                }
                if (TextUtils.isEmpty(et_input.getText().toString())) {
                    str = "";
                    et_input.setText("");
                    return;
                }
                if (DataSupport.findAll(PasswordBean.class).size() > 0 && DataSupport.findAll(PasswordBean.class) != null) {
                    datas = new ArrayList<>();
                    datas = DataSupport.findAll(PasswordBean.class);
                    if ((et_input.getText().toString().equals(datas.get(0).getPassword()))) {
//                        openActivity(MainActivity.class);
                        finish();
                    }
                } else {
                    if (poss) {
                        firstPoss = et_input.getText().toString();
                        et_input1.setHint("请再次输入密码");
                        et_input.setText("");
                        poss = false;
                    } else {
                        if (firstPoss.equals(et_input.getText().toString())) {
                            PasswordBean passwordBean = new PasswordBean();
                            passwordBean.setPassword(et_input.getText().toString() + "");
                            passwordBean.save();
                            showShortToast("密码保存成功");
                            et_input1.setHint("");
//                    openActivity(MainActivity.class);
                            finish();
                        } else
                            showShortToast("密码不一致，请重新输入");
                        et_input1.setHint("请输入密码");
                        poss = true;
                        et_input.setText("");
                        firstPoss = "";
                        return;
                    }
                    return;
                }
            {

                {
                    if (et_input.equals("0")) {
                        return;
                    } else {
                        double temp = Double.parseDouble(et_input.getText().toString());
                        temp = temp / 100;
                        et_input.setText(String.valueOf(temp));
                    }
                }
            }
            break;
        }
    }

    private void getResult() {
        String exp = et_input.getText().toString();
        et_input1.setText(exp);
        if (exp == null || exp.equals(""))
            return;
        //因为没有运算符所以不用运算
        if (!exp.contains(" ")) {
            return;
        }
        if (clr_flag) {
            clr_flag = false;
            return;
        }
        clr_flag = true;
        //截取运算符前面的字符串
        String s1 = exp.substring(0, exp.indexOf(" "));
        //截取的运算符
        String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);
        //截取运算符后面的字符串
        String s2 = exp.substring(exp.indexOf(" ") + 3);
        double cnt = 0;
        if (!s1.equals("") && !s2.equals("")) {
            double d1 = Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            if (op.equals("＋")) {
                cnt = d1 + d2;
            }
            if (op.equals("－")) {
                cnt = d1 - d2;
            }
            if (op.equals("×")) {
                cnt = d1 * d2;
            }
            if (op.equals("÷")) {
                if (d2 == 0) cnt = 0;
                else cnt = d1 / d2;
            }
            if (!s1.contains(".") && !s2.contains(".") && !op.equals("÷")) {
                int res = (int) cnt;
                et_input.setText(res + "");
            } else {
                et_input.setText(cnt + "");
            }
        }
        //如果s1是空    s2不是空  就执行下一步
        else if (!s1.equals("") && s2.equals("")) {
            double d1 = Double.parseDouble(s1);
            if (op.equals("+")) {
                cnt = d1;
            }
            if (op.equals("-")) {
                cnt = d1;
            }
            if (op.equals("×")) {
                cnt = 0;
            }
            if (op.equals("÷")) {
                cnt = 0;
            }
            if (!s1.contains(".")) {
                int res = (int) cnt;
                et_input.setText(res + "");
            } else {
                et_input.setText(cnt + "");
            }
        }
        //如果s1是空    s2不是空  就执行下一步
        else if (s1.equals("") && !s2.equals("")) {
            double d2 = Double.parseDouble(s2);
            if (op.equals("＋")) {
                cnt = d2;
            }
            if (op.equals("－")) {
                cnt = 0 - d2;
            }
            if (op.equals("×")) {
                cnt = 0;
            }
            if (op.equals("÷")) {
                cnt = 0;
            }
            if (!s2.contains(".")) {
                int res = (int) cnt;
                et_input.setText(res + "");
            } else {
                et_input.setText(cnt + "");
            }
        } else {
            et_input.setText("");
        }

    }
}
